package cn.happy.dao.impl;

import cn.happy.dao.IBookDAO;
import cn.happy.pojo.Bookinfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class BookInfoDAOImpl implements IBookDAO {

    @Override
    public List<Bookinfo> getBooks() throws Exception {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        //SqlSessionFactoryBuilder 工厂的构建器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //构建SessionFactory工厂
        SqlSessionFactory factory=builder.build(is);
        //通过工厂构建Session对象
        SqlSession sqlSession=factory.openSession();
        //使用SqlSession的selectList()进行执行sql
        List<Bookinfo> list=sqlSession.selectList("getBooks");
        return list;
    }
}
